package com.data.center.service.impl;

import com.data.center.dictionary.AdminDivisionRegion;
import com.data.center.dictionary.AdminDivisionStatus;
import com.data.center.entity.AdminDivision;
import com.data.center.exception.exception.BizException;
import com.data.center.exception.info.CommonEnum;
import com.data.center.mapper.AdminDivisionMapper;
import com.data.center.service.AdminDivisionService;
import com.data.center.util.AdminDivisionUtils;
import com.data.center.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class AdminDivisionServiceImpl implements AdminDivisionService {

    @Resource
    private AdminDivisionMapper adminDivisionMapper;

    @Autowired
    private AdminDivisionUtils adminDivisionUtils;

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    @Override
    public void createAdminDivision(AdminDivision adminDivision) {
        adminDivisionMapper.insert(adminDivision);
    }

    @Override
    public void updateAdminDivision(AdminDivision adminDivision) {
        adminDivisionMapper.update(adminDivision);
    }

    @Override
    public void deleteAdminDivision(String id) {
        adminDivisionMapper.delete(id);
    }

    @Override
    public AdminDivision getAdminDivisionById(String id) {
        return adminDivisionMapper.getById(id);
    }

    //根据行政区域编码获取区域名称
    @Override
    public String getRegionNameByCode(String code) {
        if(code.isEmpty()){
            throw new BizException(CommonEnum.BODY_NOT_MATCH);
        }
        String key = code.substring(0, 2);
        AdminDivisionRegion adminDivisionRegion = AdminDivisionRegion.getObjectByKey(key);
        return adminDivisionRegion.getValue();
    }

    @Override
    public List<AdminDivision> searchByName(String name) {
        return adminDivisionMapper.searchByName(name);
    }

    @Override
    public AdminDivision getByName(String name) {
        return adminDivisionMapper.getByName(name);
    }

    @Override
    public AdminDivision getByCode(String code) {
        return adminDivisionMapper.getByCode(code);
    }

    /*
    * 根据状态查询行政编码
    * */
    public List<AdminDivision> listAllByStatus(int status) {
        return adminDivisionMapper.listAllByStatus(status);
    }

    /*
     * 添加一个全新的行政区域编码，若库里已有该编码则进行核对比较
     * divisionCode: 六位行政区域编码
     * */
    @Override
    public AdminDivision addNewOrUpdate(String divisionCode, String name) {
        AdminDivision newAdminDivision = buildSixData(divisionCode, name, new Date());
        AdminDivision originalAdminDivision = adminDivisionMapper.getByCode(newAdminDivision.getCode());

        if (originalAdminDivision != null) {
            if (!originalAdminDivision.getName().equals(newAdminDivision.getName())) {
                throw new BizException("编码与名称发生变更，请核对:旧数据：\n"
                        + originalAdminDivision
                        + "\n新数据：" + newAdminDivision.getCode()
                        + "  " + newAdminDivision.getName());
            }

            return originalAdminDivision;
        } else {
            return add(newAdminDivision);
        }
    }

    /*
     * 添加一个全新的行政区域编码，若库里已有该编码则进行核对比较
     * divisionCode: 八位行政区域编码
     * */
    @Override
    public AdminDivision addNewOrUpdateWithEight(String divisionCode, String name, Date updateDate) {
        AdminDivision originalAdminDivision = adminDivisionMapper.getByCode(divisionCode);

        if (originalAdminDivision != null) {
//            if (!originalAdminDivision.getName().equals(name)) {
//                throw new BizException("编码与名称发生变更，请核对:旧数据：\n"
//                        + originalAdminDivision
//                        + "\n新数据：" + name);
//            }
            // 更新编码名称
            originalAdminDivision.setName(name);
            adminDivisionMapper.update(originalAdminDivision);

            return originalAdminDivision;
        } else {
            AdminDivision newAdminDivision = buildEightData(divisionCode, name, updateDate);
            return add(newAdminDivision);
        }
    }

    /*
    * 添加一个全新的行政区域编码
    * 六位行政编码
    * */
    @Override
    public AdminDivision addNew(String divisionCode, String name) {
        AdminDivision newAdminDivision = buildSixData(divisionCode, name, new Date());

        if (adminDivisionMapper.getByCode(newAdminDivision.getCode()) != null) {
            throw new BizException(CommonEnum.ADMIN_DIVISION_CODE_EXIST);
        }

        return add(newAdminDivision);
    }

    @Override
    public AdminDivision add(AdminDivision data) {
        data.setId(String.valueOf(snowflakeIdWorker.nextId()));
        adminDivisionMapper.insert(data);
        return data;
    }

    /*
     * @param: code 八位区域编码 expiredDate 失效时间
     * 将失效时间设置为当前时间
     * */
    @Override
    public AdminDivision expireNow(String code) {
        return expire(code, new Date());
    }

    /*
    * @param: code 八位区域编码 expiredDate 失效时间
    * */
    @Override
    public AdminDivision expire(String code, Date expiredDate) {
        AdminDivision originalAdminDivision = adminDivisionMapper.getByCode(code);

        if (originalAdminDivision == null) {
            throw new BizException(CommonEnum.ADMIN_DIVISION_CODE_NOT_EXIST);
        }

        originalAdminDivision.setStatus(Integer.parseInt(AdminDivisionStatus.EXPIRED.getKey()));
        originalAdminDivision.setExpiryDate(expiredDate);

        adminDivisionMapper.update(originalAdminDivision);

        return originalAdminDivision;
    }

    /*
    * 根据行政区域编码、行政区域名称，自动解析并填入对应信息
    * 输入区域编码为六位行政编码
    * */
    public AdminDivision buildSixData(String divisionCode, String name, Date effectiveDate) {
        AdminDivision adminDivision = new AdminDivision();

        adminDivision.setName(name);
        adminDivision.setCode(adminDivisionUtils.getDivisionFatherCode(divisionCode).substring(0, 2) + divisionCode);
        adminDivision.setLevel(Integer.parseInt(adminDivisionUtils.getDivisionCodeLevel(divisionCode).getKey()));
        adminDivision.setParent(adminDivisionUtils.getFatherCode(divisionCode));
        adminDivision.setStatus(Integer.parseInt(AdminDivisionStatus.IN_EFFECT.getKey()));
        adminDivision.setEffectiveDate(effectiveDate);
//        adminDivision.setExpiryDate();

        return adminDivision;
    }

    /*
     * 根据行政区域编码、行政区域名称，自动解析并填入对应信息
     * 输入区域编码为八位编码
     * */
    public AdminDivision buildEightData(String divisionCode, String name, Date effectiveDate) {
        AdminDivision adminDivision = new AdminDivision();

        adminDivision.setName(name);
        adminDivision.setCode(divisionCode);
        adminDivision.setLevel(Integer.parseInt(adminDivisionUtils.getCodeLevel(divisionCode).getKey()));
        adminDivision.setParent(adminDivisionUtils.getFatherCode(divisionCode));
        adminDivision.setStatus(Integer.parseInt(AdminDivisionStatus.IN_EFFECT.getKey()));
        adminDivision.setEffectiveDate(effectiveDate);
//        adminDivision.setExpiryDate();

        return adminDivision;
    }


}
